<?php

namespace common\widgets\tree;

use kartik\tree\models\Tree;
use kartik\tree\Module;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

/**
 * Class TreeView
 *
 * @package common\widgets\tree
 */
class TreeView extends \kartik\tree\TreeView
{

    /**
     * @var array
     */
    public $detailParams = [];

    /**
     * @const string 默认模板
     */
    const DEFAULT_FORM_VIEW = '@common/views/widgets/tree-manager/_form';

    /**
     * {@inheritdoc}
     */
    public $showIDAttribute = false;

    /**
     * {@inheritdoc}
     */
    public $iconEditSettings = [
        'show'     => 'list',
        'listData' => [
            'folder' => 'Folder',
            'file'   => 'File',
            'mobile' => 'Phone',
            'bell'   => 'Bell',
        ],
    ];

    /**
     * {@inheritdoc}
     */
    public $mainTemplate = <<< HTML
<div class="row">
    <div class="col-sm-4">
        {wrapper}
    </div>
    <div class="col-sm-8">
        {detail}
    </div>
</div>
HTML;

    /**
     * {@inheritdoc}
     */
    public function renderDetail()
    {
        /**
         * @var Tree $modelClass
         */
        $modelClass = $this->query->modelClass;
        $node = $modelClass::findOne($this->displayValue);
        if (empty($node))
        {
            $msg = Html::tag('div', $this->emptyNodeMsg, $this->emptyNodeMsgOptions);
            return Html::tag('div', $msg, $this->detailOptions);
        }
        $iconTypeAttribute = ArrayHelper::getValue($this->_module->dataStructure, 'iconTypeAttribute', 'icon_type');
        if ($this->_iconsList !== false)
        {
            $node->$iconTypeAttribute = ArrayHelper::getValue($this->iconEditSettings, 'type', self::ICON_CSS);
        }
        $params = $this->_module->treeStructure + $this->_module->dataStructure + [
                'node'            => $node,
                'action'          => $this->nodeActions[Module::NODE_SAVE],
                'formOptions'     => $this->nodeFormOptions,
                'modelClass'      => $modelClass,
                'currUrl'         => Yii::$app->request->url,
                'isAdmin'         => $this->isAdmin,
                'iconsList'       => $this->_iconsList,
                'softDelete'      => $this->softDelete,
                'showFormButtons' => $this->showFormButtons,
                'showIDAttribute' => $this->showIDAttribute,
                'nodeView'        => $this->nodeView,
                'nodeAddlViews'   => $this->nodeAddlViews,
                'breadcrumbs'     => $this->breadcrumbs,
            ] + $this->detailParams;
        $content = $this->render($this->nodeView, ['params' => $params]);
        return Html::tag('div', $content, $this->detailOptions);
    }
}
